package com.example.mongo.appmongo.controller;

import com.allqj.qjf.multi.datasource.utils.TenantContextHolder;
import com.example.mongo.appmongo.service.HomeService;
import com.example.mongo.appmongo.vo.ElasticSearchConfigVO;
import com.example.mongo.appmongo.vo.MySQLConfigVO;
import com.example.mongo.appmongo.vo.ReqVO;
import com.example.mongo.appmongo.vo.RespVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author sjw
 * @className HomeController
 * @date 2021/9/29 13:40
 * @description: TODO
 */
@RestController
@RequestMapping("/home/")
public class HomeController {

//    @Autowired
//    private TenantContext tenantContext;

    @Autowired
    private HomeService homeService;

    @GetMapping("/hello/{name}")
    public String hello(@PathVariable String name) {
        return homeService.insert(name);
    }

    @GetMapping("/tran/{name}/{db}/{id}")
    public String tran(@PathVariable String name, @PathVariable int db, @PathVariable String id) throws Exception {
//        if(db==1){
//            TenantContext.setTenant("transactionManager1");
//        }else TenantContext.setTenant("transactionManager");
        System.out.println(TenantContextHolder.getTenantContext());
        return homeService.tran(name, id);
    }

    @PostMapping("/dataSource")
    public RespVO mysql(@RequestBody ReqVO reqVO) throws Exception {
        System.out.println("-------------------------------------");
        if (reqVO == null || reqVO.getTenant() == null) return null;
        MySQLConfigVO vo = null;
        ElasticSearchConfigVO vo1 = null;
        if ("1".equals(reqVO.getTenant())) {
            vo = new MySQLConfigVO();
            vo.setHost("192.168.10.96");
            vo.setPort(3306);
            vo.setUserName("root");
            vo.setPassword("123456");

            vo1 = new ElasticSearchConfigVO();
            vo1.setClusterName("elasticsearch");
            vo1.setClusterNodes("192.168.10.127:9300");

        } else if ("2".equals(reqVO.getTenant())) {
            vo = new MySQLConfigVO();
            vo.setHost("192.168.10.96");
            vo.setPort(3307);
            vo.setUserName("root");
            vo.setPassword("123456");

            vo1 = new ElasticSearchConfigVO();
            vo1.setClusterName("elasticsearch");
            vo1.setClusterNodes("192.168.10.127:9300");
        }
        RespVO respVO = new RespVO();
        respVO.setMySQLConfig(vo);
        respVO.setElasticSearchConfig(vo1);
        return respVO;
    }

    @PostMapping("/es")
    public ElasticSearchConfigVO es(@RequestBody ReqVO reqVO) throws Exception {
        System.out.println("-------------------------------------");
        if (reqVO == null || reqVO.getTenant() == null) return null;
        ElasticSearchConfigVO vo1 = null;
        if ("1".equals(reqVO.getTenant())) {
            vo1 = new ElasticSearchConfigVO();
            vo1.setClusterName("elasticsearch");
            vo1.setClusterNodes("192.168.10.127:9300");

        } else if ("2".equals(reqVO.getTenant())) {
            vo1 = new ElasticSearchConfigVO();
            vo1.setClusterName("elasticsearch");
            vo1.setClusterNodes("192.168.10.127:9300");
        }
        return vo1;
    }

}
